package com.kky.pp.util

import android.animation.Animator
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.core.view.isVisible
import com.kky.pp.R
import com.kky.pp.adapter.CardStackAdapter
import com.kky.pp.view.cardstackview.CardStackLayoutManager
import com.kky.pp.view.cardstackview.Direction
import com.luck.picture.lib.utils.DensityUtil
import com.luck.picture.lib.utils.SpUtils


fun FrameLayout.likeOrDisLikeClickAnimation(
    adapter: CardStackAdapter,
    direction: Direction,
    manager: CardStackLayoutManager,
) {
    if (adapter.turnOnsLimit == manager.topPosition) {
        return
    }
    manager.isLikeOrDisLikeClick = true
    val maxValue = DensityUtil.dip2px(this.context, 142f)
    val animator = ValueAnimator.ofInt(0, maxValue)
    animator.duration = 500
    var leftPadding = 0
    var rightPadding = 0
    val view = this
    animator.addUpdateListener {
        val curValue: Int = it.animatedValue as Int
        when (direction) {
            Direction.Left -> {
                leftPadding = curValue
            }

            Direction.Right -> {
                rightPadding = curValue
            }

            else -> {}
        }
        view.setPadding(leftPadding, 0, rightPadding, 0)
        if (curValue >= maxValue) {
            this.animate().alpha(0f).setDuration(500)
                .setListener(object : Animator.AnimatorListener {
                    override fun onAnimationStart(animation: Animator) {}
                    override fun onAnimationEnd(animation: Animator) {
                        view.setPadding(0, 0, 0, 0)
                        view.alpha = 1f
                        manager.isLikeOrDisLikeClick = false
                    }
                    override fun onAnimationCancel(animation: Animator) {}
                    override fun onAnimationRepeat(animation: Animator) {}
                }).start()
        }
    }
    animator.start()
}

fun FrameLayout.likeOrDisLikeAnimation(
    adapter: CardStackAdapter,
    viewDisLike: FrameLayout,
    cardManager: CardStackLayoutManager
) {
    cardManager.setScrollSlideListener { direction, slideY ->
        run {
            if (cardManager.isLikeOrDisLikeClick || adapter.turnOnsLimit == cardManager.topPosition) {
                return@run
            }
            val sY = DensityUtil.dip2px(this.context, 150f)
            val padding = sY * slideY
            var view: FrameLayout? = null
            var leftPadding = 0
            var rightPadding = 0
            when (direction) {
                Direction.Left -> {
                    view = viewDisLike
                    leftPadding = padding.toInt()
                }

                Direction.Right -> {
                    view = this
                    rightPadding = padding.toInt()
                }

                else -> {}
            }
            view?.setPadding(leftPadding, 0, rightPadding, 0)
            view?.alpha = slideY
            if (slideY >= 1.2f) {
//                view?.setPadding(0, 0, 0, 0)
//                view?.alpha = 0f
            }
        }
    }
}

fun View.setClick(time: Long = 200, listener: View.OnClickListener) {
    this.setOnClickListener {
        if (DoubleClickUtils.clickAble(time)) {
            listener.onClick(it)
        }
    }

}

fun View.startAnimate(viewOwn: FrameLayout, viewLike: FrameLayout, containerBackArrow: ImageView, imgMatchHeart: ImageView) {

    this.post {
        val translationY = -this.height * 1f
        val animator: ObjectAnimator =
            ObjectAnimator.ofFloat(
                this,
                "translationY",
                0f,
                translationY,
                translationY + 50f,
                translationY
            )
        animator.duration = 2000
        animator.start()
    }
    viewOwn.post {
        val realScreenWidth = DensityUtil.getRealScreenWidth(viewOwn.context)
        val translationX =realScreenWidth/2*1f
//            (viewOwn.width + com.kky.pp.util.DensityUtil.dp2px(viewOwn.context, 58f)) * 1f
        val animator: ObjectAnimator =
            ObjectAnimator.ofFloat(
                viewOwn,
                "translationX",
                0f,
                translationX,
                translationX - 50f,
                translationX
            )
        animator.duration = 2000
        animator.start()
    }
    viewLike.post {
        val realScreenWidth = DensityUtil.getRealScreenWidth(viewOwn.context)
        val translationX =-realScreenWidth/2*1f
//            -(viewOwn.width + com.kky.pp.util.DensityUtil.dp2px(viewLike.context, 58f)) * 1f
        val animator: ObjectAnimator =
            ObjectAnimator.ofFloat(
                viewLike,
                "translationX",
                0f,
                translationX,
                translationX + 50f,
                translationX
            )
        animator.duration = 2000
        animator.start()
    }
    containerBackArrow.animate().alpha(1f).setDuration(2000).start()
    imgMatchHeart.animate().alpha(1f).setDuration(2000).start()
}

fun ImageView.initGuide(guideEnd: () -> Unit) {
    this.isVisible = !SpUtils.getBoolean(this.context, SpUtils.IS_OPEN_HOME_GUIDE, false)
    SpUtils.putBoolean(this.context, SpUtils.IS_OPEN_HOME_GUIDE, true)
    this.setOnClickListener {
        if (this.getTag() == null) {
            this.setTag(1)
            this.setImageResource(R.mipmap.icon_guide_two)
        } else {
            val intTag = this.getTag() as Int
            if (intTag == 1) {
                this.setTag(2)
                this.setImageResource(R.mipmap.icon_guide_three)
            }
            if (intTag == 2) {
                this.isVisible = false
                guideEnd.invoke()
            }
        }
    }
}
